package com.core.family.admin.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.core.common.gen.BaseServiceImpl;
import com.core.family.admin.domain.po.SysMenu;
import com.core.family.admin.domain.po.SysRoleMenu;
import com.core.family.admin.mapper.SysMenuMapper;
import com.core.family.admin.mapper.SysRoleMenuMapper;
import com.core.family.admin.service.SysRoleMenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;


/**
 * @author honglei
 * @version 1.0
 * @time: 2022-6-10 11:46:34
 * @Description 服务实现层
 */
@Service
public class SysRoleMenuServiceImpl extends BaseServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {


    @Resource
    SysMenuMapper menuMapper;

    @Override
    public List<SysRoleMenu> getByRoleIds(Serializable orgId, List<Integer> roleIds) {
        LambdaQueryWrapper<SysRoleMenu> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysRoleMenu::getOrgId, orgId)
                .in(SysRoleMenu::getRoleId, roleIds);
        return this.baseMapper.selectList(wrapper);
    }

    @Override
    public boolean bangAllMenus(Integer orgId, Integer roleId) {
        List<SysMenu> menuList = Optional.ofNullable(
                menuMapper.selectList(Wrappers.lambdaQuery(SysMenu.class)
                        // .eq(SysMenu::getOrgId,orgId)
                        .eq(Objects.nonNull(roleId), SysMenu::getId, roleId))
        ).orElse(new ArrayList<>());

        List<SysRoleMenu> list = new ArrayList<>();

        for (SysMenu menu : menuList) {
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setOrgId(orgId);
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menu.getId());
            list.add(roleMenu);
        }
        if (list.size() != 0) {
            saveBatch(list);
        }
        return true;
    }
}

